package projectiles;

import java.awt.Dimension;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;

import core.Model;
import core.ShootingUnit;
import core.Unit;
import core.Vector;

/**
 * @author Robbie Groenewoudt
 * @version 1.1
 * @since 2011-04-11
 */
public abstract class Projectile extends Unit {

	protected int damage;
	protected Point2D.Double destination;
	protected int speed;
	protected Vector direction;
	protected ShootingUnit origin;

	/**
	 * 
	 * @param origin het beginpunt
	 * @param position de positie
	 * @param viewAngle de richting van het projectiel
	 * @param size de groote
	 * @param image het plaatje
	 * @param damage de schade die het projectiel aanricht
	 * @param destination het eindpunt
	 * @param speed de snelheid.
	 */
	/* /// Constructor //////////////////////////////////////// */
	public Projectile(ShootingUnit origin, Point2D.Double position, int viewAngle, Dimension size, ImageIcon image,
			int damage, Point2D.Double destination, int speed) {
		// Projectile heeft geen health
		super(position, 0, image, size, viewAngle);

		this.damage = damage;
		this.destination = destination;
		this.speed = speed;
		this.origin = origin;

		// Bereken direction
		// We moeten de vector normalizen of anders zit er ook een speed in
		this.direction = new Vector(destination.x - position.x, destination.y - position.y).normalize();

		// Maak bounds
		calcBounds();
	}

	/* /// getDamage //////////////////////////////////////// */
	public int getDamage() {
		return damage;
	}

	/* /// setDamage //////////////////////////////////////// */
	public void setDamage(int damage) {
		this.damage = damage;
	}

	/* /// getDestination //////////////////////////////////////// */
	public Point2D.Double getDestination() {
		return destination;
	}

	/* /// setDestination //////////////////////////////////////// */
	public void setDestination(Point2D.Double destination) {
		this.destination = destination;
	}

	/* /// getSpeed //////////////////////////////////////// */
	public int getSpeed() {
		return speed;
	}

	/* /// setSpeed //////////////////////////////////////// */
	public void setSpeed(int speed) {
		this.speed = speed;
	}

	/**
	 * Update positie en bounds
	 * @see core.Unit#update()
	 *
	 * @return altijd true
	 */
	@Override
	/* /// update //////////////////////////////////////// */
	public boolean update() {
		// Projectiel beweegt naar bestemming
		double deltaSpeed = this.speed * Model.timerInterval;
		setPosition(new Point2D.Double(this.position.x + (direction.x * deltaSpeed), this.position.y
				+ (direction.y * deltaSpeed)));

		// Papa update bounds
		super.update();

		return true;
	}

	/* /// getOrigin //////////////////////////////////////// */
	public ShootingUnit getOrigin() {
		return origin;
	}
}